package service

import (
	"ProjectOne/projectDemo/CustomerManagementSystem/model"
	"fmt"
)

//完成对customer的操作
//CRUD
type CustomerService struct {
	customers []model.Customer
	//当前切片的客户数量
	customerNum int
}

func NewCustomerService() *CustomerService {
	customerService := &CustomerService{}
	customerService.customerNum = 1
	customer := model.NewCustomer(1, "张三", "男", 20, "110", "123456@qq.com")
	customerService.customers = append(customerService.customers, *customer)
	return customerService
}
func (this *CustomerService) List() []model.Customer {
	return this.customers
}
func (this *CustomerService) Add(customer model.Customer) bool {
	//确定分配id的规则，就是添加的顺序
	this.customerNum++
	customer.Id = this.customerNum
	this.customers = append(this.customers, customer)
	return true
}

/**
根据id查找客户再切片的下标，没有则返回-1
*/
func (this *CustomerService) FindById(id int) int {
	index := -1
	//遍历切片
	for i := 0; i < len(this.customers); i++ {
		if this.customers[i].Id == id {
			index = i
		}
	}
	return index
}

//根据id删除客户
func (this *CustomerService) Delete(id int) bool {
	index := this.FindById(id)
	if index == -1 {
		fmt.Println("没有该客户")
		return false
	} else {
		this.customers = append(this.customers[:index], this.customers[index+1:]...)
		return true
	}
}
func (this *CustomerService) Modify(id int) (customer *model.Customer) {
	index := this.FindById(id)
	return &this.customers[index]
}
